package com.larry.design.creatation.prototype;

import java.util.HashMap;
import java.util.Map;

/**
 * @author larry
 * @Title: design-frame
 * @Package com.larry.design.creatation.prototype
 * @Description: TODO
 * @date 8/31/2112:50 PM
 */
public class FindUser {
    //缓存
    Map<String,User> userCache = new HashMap<String, User>();

    public User getUser(String username) throws CloneNotSupportedException {
        User user = null;
        if (!userCache.containsKey(username)){
            user = getUserFromDb(username);
        }else {
            user = userCache.get(username);
            user = (User) user.clone();
        }

        return user;
    }

    private User getUserFromDb(String username) throws CloneNotSupportedException {
        System.out.println("模拟数据库查到....");
        User user = new User();
        user.setUsername(username);
        user.setAge(12);
        user = (User) user.clone();
        userCache.put(username,user);
        return user;
    }
}
